package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.entity.Area;
import com.shike.hq.entity.Group;
import com.shike.hq.service.AreaService;
import com.shike.hq.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * The type Group controller.
 */
@Controller
@RequestMapping("/base")
public class GroupController {

  private static final String REDIRECT = "redirect:/base/group";
  @Autowired
  GroupService groupService;

  @Autowired
  AreaService areaService;

  /**
   * 巡检组页面路径映射
   *
   * @param model   model类型
   * @param session session类型
   * @return 页面路径映射字段
   */
  @GetMapping("/group")
  public String group(Model model, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);
    if (groups.size() > 0) {
      //找到巡检组信息
      model.addAttribute("groups", groups);
    } else {
      model.addAttribute("groups", "null");
    }
    ArrayList<Area> areas = areaService.selectAllAreaById(areaId);
    model.addAttribute("areas", areas);
    return "base/group";
  }

  /**
   * 编辑巡检组信息
   *
   * @param group 编辑后的巡检组信息
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @PostMapping("/group/update")
  public String updateGroup(Group group, RedirectAttributes attributes) {
    try {
      groupService.updateById(group);
      attributes.addFlashAttribute("msg", "编辑成功！");
    } catch (Exception e){
      e.printStackTrace();
      attributes.addFlashAttribute("msg", "编辑失败！");
    }
    return REDIRECT;
  }

  /**
   * 删除巡检组信息
   *
   * @param id    要删除的巡检组id
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/group/delete")
  public String deleteGroup(@RequestParam("id") long id, RedirectAttributes attributes) {
    try  {
      groupService.removeById(id);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e){
      attributes.addFlashAttribute("msg", "删除失败！该巡检组下还有巡检员！");
    }
    return REDIRECT;
  }

  /**
   * 删除多条巡检组信息
   *
   * @param idList     巡检组id列表
   * @param attributes model类型
   * @return 页面路径字符Get串
   */
  @GetMapping("/group/deleteGroups")
  public String deleteAreas(@RequestParam("idList") ArrayList<Long> idList,
                            RedirectAttributes attributes) {
    try {
      groupService.removeByIds(idList);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！部分巡检组下存在巡检员！");
    }
    return REDIRECT;
  }

  /**
   * 添加巡检组信息
   *
   * @param group      添加的巡检组信息
   * @param attributes model类型
   * @param session    session对象
   * @return
   */
  @PostMapping("/group/add")
  public String addGroup(Group group, RedirectAttributes attributes, HttpSession session) {
    group.setCreateTime(new Timestamp(System.currentTimeMillis()));
    try {
      groupService.save(group);
      attributes.addFlashAttribute("msg", "添加成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "添加失败！");
    }
    return REDIRECT;
  }
}
